﻿using AnlianCommon.Common.Domains.BreedingManagement;
using AnlianCommon.Common.Domains.SheepManagement;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.SheepCommands.SheepDiees
{
    public class SheepDieQueryHandler : IRequestHandler<SheepDieQueryCommand, ResultPage<SheepDie>>
    {
        private readonly IBaseRepository<SheepDie> _sheepDieRepository;

        public SheepDieQueryHandler(IBaseRepository<SheepDie> sheepDieRepository)
        {
            _sheepDieRepository = sheepDieRepository;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<SheepDie>> Handle(SheepDieQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<SheepDie>() { Code = (int)CommonErrorCode.Search, TotalCount = 0, PagedCount = 0 };
            try
            {
                var sheepDies = await (from a in await _sheepDieRepository.GetAll()
                                       select new SheepDie()
                                       {
                                           SheepDieId = a.SheepDieId,
                                           DieDate = a.DieDate,
                                           SheepNumber = a.SheepNumber,
                                           DieReason = a.DieReason,
                                           SheepBreed = a.SheepBreed,
                                           SheepSex = a.SheepSex,
                                           ReproductiveState = a.ReproductiveState,
                                           DieMonth = a.DieMonth,
                                           DieDay = a.DieDay,
                                           DieHouse = a.DieHouse,
                                           DieField = a.DieField,
                                           SheepBirthday = a.SheepBirthday,
                                           CreatedOwnerId = a.CreatedOwnerId,
                                           CreatedTime = a.CreatedTime,
                                       }).ToListAsync();

                if (request.StartDieDate != null)
                {
                    sheepDies = sheepDies.Where(c => c.DieDate >= request.StartDieDate).ToList();
                }
                if (request.EndDieDate != null)
                {
                    sheepDies = sheepDies.Where(c => c.DieDate <= request.EndDieDate).ToList();
                }
                if (!string.IsNullOrEmpty(request.SheepNumber))
                {
                    sheepDies = sheepDies.Where(c => c.SheepNumber == request.SheepNumber).ToList();
                }
                if (request.DieReason > 0)
                {
                    sheepDies = sheepDies.Where(c => c.DieReason == request.DieReason).ToList();
                }
                result.TotalCount = sheepDies.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                sheepDies = sheepDies.OrderBy(c => c.SheepDieId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = sheepDies;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
